package com.ztgf.sms.service.sms;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.Md5Utils;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.sms.common.config.YmlConfig;
import com.ztgf.sms.mapper.sms.SmsUserMapper;
import com.ztgf.sms.models.bo.SmsUserSaveBO;
import com.ztgf.sms.models.dto.sms.SmsUserDTO;
import com.ztgf.sms.models.service.ISmsUserService;
import com.ztgf.sms.models.vo.SmsUserVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 文件用户表控制器
 *
 * @author mxli
 * @Date 2020-10-15 14:32:14
 */
@DubboService
public class SmsUserServiceImpl extends ServiceImpl<SmsUserMapper, SmsUserDTO> implements ISmsUserService {

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean add(SmsUserSaveBO saveBO) {
        SmsUserDTO dto = new SmsUserDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);

        String salt = Md5Utils.createSalt();
        dto.setId(id);
        dto.setPassword(Md5Utils.MD5Encode(saveBO.getPassword().toUpperCase(), salt));
        dto.setSalt(salt);

        return this.insert(dto);
    }



    @Override
    public SmsUserDTO getUserBySession(String sessionId) {
        Object bean = redisUtil.getSession(sessionId, CommonParameter.getHeadParamsBO().getSource().toString());
        if (bean != null && bean instanceof SmsUserDTO) {
            return (SmsUserDTO) bean;
        }
        return null;
    }

    @Override
    public SmsUserDTO getUserBySession(Long userId) {
        String source = CommonParameter.getHeadParamsBO().getSource().toString();
        Object sessionId = redisUtil.getIdSessionRelation(userId + "", source);
        if(sessionId == null){
            return null;
        }
        return getUserBySession(sessionId.toString());
    }

    @Override
    public void setUserSession(String sessionId, SmsUserDTO dto) {
        String source = CommonParameter.getHeadParamsBO().getSource().toString();
        if(dto != null){
            Object oldSessionId = redisUtil.getIdSessionRelation(dto.getId().toString(), source);
            if(oldSessionId != null){
                redisUtil.removeSession(oldSessionId.toString(), source);
            }
        }
        redisUtil.setSession(sessionId, dto, source);
        redisUtil.setIdSessionRelation(dto.getId().toString(), sessionId, source);
    }

    @Override
    public void delUserSession(String sessionId) {
        SmsUserDTO dto = getUserBySession(sessionId);
        if(dto != null){
            String source = CommonParameter.getHeadParamsBO().getSource().toString();
            redisUtil.removeIdSession(dto.getId().toString(), source);
            redisUtil.removeSession(sessionId, source);
        }
    }

    @Override
    public void delUserSessionById(Long id) {
        SmsUserDTO SmsUserDTO = selectById(id);
        if(SmsUserDTO != null){
            String source = CommonParameter.getHeadParamsBO().getSource().toString();
            Object idSessionRelation = redisUtil.getIdSessionRelation(id.toString(), source);
            if(null == idSessionRelation){
                return;
            }
            redisUtil.removeIdSession(id.toString(), source);
            redisUtil.removeSession(idSessionRelation.toString(), source);
        }
    }

    @Override
    public boolean setResetUserSession(String sessionId) {
        SmsUserDTO dto = getUserBySession(sessionId);
        if(dto != null){
            String source = CommonParameter.getHeadParamsBO().getSource().toString();
            redisUtil.setSession(sessionId, dto, source);
            redisUtil.setIdSessionRelation(dto.getId().toString(), sessionId, source);
            return true;
        }
        return false;
    }

    /**
     *
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @param name 用户名字
     * @return
     */
    @Override
    public Result updateUserPassword(String oldPassword,String newPassword,String name){
        String sessionId = CommonParameter.getHeadParamsBO().getSessionId();
        SmsUserDTO SmsUserDTO = getUserBySession(sessionId);
        if(null == SmsUserDTO){
            return new Result(CodeEnum.FAIL_2);
        }
        SmsUserDTO = selectById(SmsUserDTO.getId());
        if(!Objects.equals(SmsUserDTO.getPassword(),
                Md5Utils.MD5Encode(oldPassword.toUpperCase(), SmsUserDTO.getSalt()))){
            return new Result(CodeEnum.FAIL_USER_200001);
        }
        SmsUserDTO.setPassword(Md5Utils.MD5Encode(newPassword.toUpperCase(), SmsUserDTO.getSalt()));
        if(StrUtils.isNotEmpty(name)){
            SmsUserDTO.setName(name);
        }
        updateById(SmsUserDTO);
        return new Result();
    }

    @Override
    public Page<SmsUserVO> queryPageVOList(Page<SmsUserVO> page, Wrapper<SmsUserVO> wrapper, Integer type) {
        List<SmsUserVO> list = this.baseMapper.queryPageVOList(page, wrapper,type);
        if(list == null){
            list = new ArrayList<>();
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public SmsUserVO getUserVO(Long userId) {
        return this.baseMapper.getUserVO(userId);
    }
}
